package com.ruoyi.reportsnhnt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportsnhnt.domain.bo.ReportSnhntKyqdBaDetailBo;
import com.ruoyi.reportsnhnt.domain.vo.ReportSnhntKyqdBaDetailVo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntKyqdBaDetail;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntKyqdBaDetailMapper;
import com.ruoyi.reportsnhnt.service.IReportSnhntKyqdBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 水泥混凝土抗压强度试验检测记录详情Service业务层处理
 *
 * @author zxs
 * @date 2024-06-21
 */
@RequiredArgsConstructor
@Service
public class ReportSnhntKyqdBaDetailServiceImpl implements IReportSnhntKyqdBaDetailService {

    private final ReportSnhntKyqdBaDetailMapper baseMapper;

    /**
     * 查询水泥混凝土抗压强度试验检测记录详情
     */
    @Override
    public ReportSnhntKyqdBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询水泥混凝土抗压强度试验检测记录详情列表
     */
    @Override
    public TableDataInfo<ReportSnhntKyqdBaDetailVo> queryPageList(ReportSnhntKyqdBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSnhntKyqdBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportSnhntKyqdBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水泥混凝土抗压强度试验检测记录详情列表
     */
    @Override
    public List<ReportSnhntKyqdBaDetailVo> queryList(ReportSnhntKyqdBaDetailBo bo) {
        LambdaQueryWrapper<ReportSnhntKyqdBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSnhntKyqdBaDetail> buildQueryWrapper(ReportSnhntKyqdBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSnhntKyqdBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportSnhntKyqdBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getShijbh() != null, ReportSnhntKyqdBaDetail::getShijbh, bo.getShijbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShejqddj()), ReportSnhntKyqdBaDetail::getShejqddj, bo.getShejqddj());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbw()), ReportSnhntKyqdBaDetail::getGongcbw, bo.getGongcbw());
        lqw.eq(bo.getChengxrq() != null, ReportSnhntKyqdBaDetail::getChengxrq, bo.getChengxrq());
        lqw.eq(bo.getShiyrq() != null, ReportSnhntKyqdBaDetail::getShiyrq, bo.getShiyrq());
        lqw.eq(StringUtils.isNotBlank(bo.getLingq()), ReportSnhntKyqdBaDetail::getLingq, bo.getLingq());
        lqw.eq(StringUtils.isNotBlank(bo.getShijcc()), ReportSnhntKyqdBaDetail::getShijcc, bo.getShijcc());
        lqw.eq(StringUtils.isNotBlank(bo.getChengymj()), ReportSnhntKyqdBaDetail::getChengymj, bo.getChengymj());
        lqw.eq(StringUtils.isNotBlank(bo.getShictld()), ReportSnhntKyqdBaDetail::getShictld, bo.getShictld());
        lqw.eq(bo.getJixhz() != null, ReportSnhntKyqdBaDetail::getJixhz, bo.getJixhz());
        lqw.eq(bo.getHuansxs() != null, ReportSnhntKyqdBaDetail::getHuansxs, bo.getHuansxs());
        lqw.eq(bo.getKangyqddz() != null, ReportSnhntKyqdBaDetail::getKangyqddz, bo.getKangyqddz());
        lqw.eq(bo.getKangyqdcdz() != null, ReportSnhntKyqdBaDetail::getKangyqdcdz, bo.getKangyqdcdz());
        lqw.eq(bo.getHuanshkyqd() != null, ReportSnhntKyqdBaDetail::getHuanshkyqd, bo.getHuanshkyqd());
        lqw.eq(StringUtils.isNotBlank(bo.getDadsjqdbfb()), ReportSnhntKyqdBaDetail::getDadsjqdbfb, bo.getDadsjqdbfb());
        return lqw;
    }

    /**
     * 新增水泥混凝土抗压强度试验检测记录详情
     */
    @Override
    public Boolean insertByBo(ReportSnhntKyqdBaDetailBo bo) {
        ReportSnhntKyqdBaDetail add = BeanUtil.toBean(bo, ReportSnhntKyqdBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改水泥混凝土抗压强度试验检测记录详情
     */
    @Override
    public Boolean updateByBo(ReportSnhntKyqdBaDetailBo bo) {
        ReportSnhntKyqdBaDetail update = BeanUtil.toBean(bo, ReportSnhntKyqdBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSnhntKyqdBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水泥混凝土抗压强度试验检测记录详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
